import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sinosheets/utils/storage/LSUtil.dart';

class ThemeUtil {
  /// 主题列表(都没有选中)
  /// 由于需要执行多语言函数，这里需要用函数，不能用静态变量
  /// 跟随系统先隐藏
  static List get themeList => [
        // {
        //   'name': 'system'.tr,
        //   'code': 'system',
        //   "mode": ThemeMode.system,
        //   'select': false,
        // },
        {
          'name': 'light'.tr,
          'code': 'light',
          "mode": ThemeMode.light,
          'select': false,
        },
        {
          'name': 'dark'.tr,
          'code': 'dark',
          "mode": ThemeMode.dark,
          'select': false,
        },
      ];

  /// 带单选状态的主题列表
  static List getThemeList() {
    List tempList = themeList;
    List newList = [];

    /// 根据本地存储的状态设置选中状态
    String code = LSUtil.getThemeCode();
    for (var theme in tempList) {
      if (code == theme["code"]) {
        theme["select"] = true;
      } else {
        theme["select"] = false;
      }
      newList.add(theme);
    }
    return newList;
  }

  /// 获得选中的Theme
  static Map getSelectTheme() {
    List tempList = themeList;
    Map selectTheme = {};

    /// 根据本地存储的状态设置选中状态
    String code = LSUtil.getThemeCode();
    for (var theme in tempList) {
      if (code == theme["code"]) {
        selectTheme = theme;
        break;
      }
    }
    return selectTheme;
  }

  /// 切换主题
  static changeTheme() {
    ThemeMode mode = getThemeMode();
    ThemeData themeData = getThemeData();
    Get.changeThemeMode(mode);
    Get.changeTheme(themeData);

    updateTheme();
  }

  static updateTheme() async {
    /// 这里需要delay，不能直接调用Get.forceAppUpdate();
    Future.delayed(const Duration(milliseconds: 200), (() {
      Get.forceAppUpdate();

      /// 状态更新后，检查模式，更新静态变量
      checkDarkMode();
    }));
  }

  /// 获取本地 主题配置
  static ThemeMode getThemeMode() {
    String code = LSUtil.getThemeCode();
    ThemeMode themeMode = ThemeMode.light;
    if (code == 'light') {
      themeMode = ThemeMode.light;
    } else if (code == 'dark') {
      themeMode = ThemeMode.dark;
    } else if (code == 'system') {
      themeMode = ThemeMode.system;
    }
    return themeMode;
  }

  static ThemeData getThemeData() {
    String code = LSUtil.getThemeCode();
    ThemeData themeData = ThemeData.light();
    if (code == 'light') {
      themeData = ThemeData(brightness: Brightness.light);
    } else if (code == 'dark') {
      themeData = ThemeData(brightness: Brightness.dark);
    } else if (code == 'system') {
      if (isDarkMode) {
        themeData = ThemeData(brightness: Brightness.dark);
      } else {
        themeData = ThemeData(brightness: Brightness.light);
      }
    }
    return themeData;
  }

  /// 记忆状态，默认不是暗黑模式
  static bool isDarkMode = false;

  /// 在需要的时候检查状态，更新静态变量
  static checkDarkMode() {
    String code = LSUtil.getThemeCode();
    if (code == 'system') {
      isDarkMode = (MediaQuery.of(Get.context!).platformBrightness == Brightness.dark);
    } else {
      isDarkMode = Get.isDarkMode;
    }
  }

  /// 对外接口，直接返回静态变量，提升性能
  static bool get isLightMode {
    return !isDarkMode;
  }
}
